package com.gutils.optimisation
{
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;

	public class FrameStatistic extends Sprite
	{
		private var stage:Stage;
		
		// graphs
		private var lineGraphCounter:int;
		private var lineGraphs:Vector.<Sprite> = new Vector.<Sprite>;
		private var pieGraph:MovieClip;
		
		// charts data provider
		private var dataProvider:Vector.<Object>;
		
		public function FrameStatistic($stage:Stage)
		{
			this.stage = $stage;
			
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
			
			setStyles();
			
		}
		
		private function onEnterFrame(event:Event):void
		{
			if (dataProvider == null)
				return;
			
			var total:Number = 0;
			
			dataProvider.forEach( function callback(item:Object, index:int, vector:Vector.<Object>):void {
				total+=Number(item.time);
			});
			
			drawGraph( 25, dataProvider, total );
			updateLabels( dataProvider );
		}
		
		private function drawGraph( radius:int, dataProvider:Vector.<Object>, total:int ):void
		{
			if (pieGraph != null)
				removeChild( pieGraph );
			
			addChild( pieGraph = new MovieClip() );
			
			var numOfRadians:Number = 0;
			pieGraph.x = 30;
			pieGraph.y = 46;
			
			pieGraph.graphics.lineStyle( 1, 0x000000 );
			var radian:Number;
			var title:String;
			
			dataProvider.forEach( function callback(item:Object, index:int, vector:Vector.<Object>):void {
				
				// draw line graph
				if ( lineGraphCounter > WIDTH - LINE_GRAPH_STARTING_POINT_X )
					resetLineGraph();
				
				lineGraphs[index].graphics.lineTo( LINE_GRAPH_STARTING_POINT_X + lineGraphCounter , ( 100-dataProvider[index].time ) );
				
				// draw pie
				radian = Number( item.time ) / total * 2;
				title = item.name;
				
				pieGraph.graphics.beginFill( colors[index] );
				pieGraph.graphics.moveTo( 0,0 );
				pieGraph.graphics.lineTo( Math.sin( numOfRadians * Math.PI ) * radius, Math.cos( numOfRadians * Math.PI ) * radius );
				
				for(var n:Number = 0; n <= radian; n += .0001)
					pieGraph.graphics.lineTo( Math.sin( ( numOfRadians+n ) * Math.PI ) * radius, Math.cos( ( numOfRadians+n ) * Math.PI ) * radius );
				
				numOfRadians += radian;
				pieGraph.graphics.lineTo( 0,0 );
				pieGraph.graphics.endFill();
				
			});
		}
		
		private function updateLabels(dataProvider:Vector.<Object>):void
		{
			pieStyle.constructorCodeExecuted = "Constructor Code: " + dataProvider[0].time; 
			pieStyle.frameActionsExecuted = "Frame Actions: " + dataProvider[1].time;
			pieStyle.finalUserCodeExecuted = "Final UserCode: " + dataProvider[2].time;
			pieStyle.playerRendersChangesDisplayList = "Display changes: " + dataProvider[3].time;
			
			if (stage != null)
				globalStyle.fps = "FPS: " + stage.frameRate;
			
			globalStyle.memory = "MEM: " + Number( ( System.totalMemory * 0.0000001 ).toFixed(2) );
			globalStyle.time = "TIME: " + countSecondsSinceStart;
			
			globalText.htmlText = globalStyle;
			pieText.htmlText = pieStyle;
		}
	}
}